package com.teemor.tim;


import com.teemor.common.bean.utils.redis.RedisUtils;
import com.teemor.tim.bean.form.openim.ImportMsgForm;
import com.teemor.tim.bean.form.openim.SendMsgForm;
import com.teemor.tim.bean.form.sns.FriendAddForm;
import com.teemor.tim.bean.form.CreateGroupForm;
import com.teemor.tim.bean.form.sns.FriendCheckForm;
import com.teemor.tim.bean.form.sns.FriendDeleteForm;
import com.teemor.tim.bean.result.openim.ImportMsgResult;
import com.teemor.tim.bean.result.openim.SendMsgResult;
import com.teemor.tim.bean.result.sns.FriendCheckResult;
import com.teemor.tim.bean.result.sns.FriendDeleteResult;
import com.teemor.tim.utils.MessageUtil;
import com.teemor.tim.model.message.MessageWrapper;
import com.teemor.tim.rest.AccountFeignClient;
import com.teemor.tim.rest.GroupFeignClient;
import com.teemor.tim.rest.OpenimFeignClient;
import com.teemor.tim.rest.SnsFeignClient;
import com.teemor.tim.rest.model.RestParam;
import com.teemor.tim.model.group.GroupAddModel;
import com.teemor.tim.bean.result.sns.FriendAddResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ename zhoulk
 * @cname 周李科
 * @date 2020/12/5 13:32
 */
@Component
public class TencentIMHelper {
    private static Long sdkAppId = 1400374910L;
    private static String key = "bf9bf836bf3a8662ab101097922e8d7d4e3139ed5867876051c14c2048afb726";

    private static final String ADMINUSER = "administrator";
    private static final String CONTENTTYPE = "json";
    private static final com.teemor.tim.utils.TLSSigAPIv2 tlsSigAPIv2 = new com.teemor.tim.utils.TLSSigAPIv2(sdkAppId, key);


    @Autowired
    private SnsFeignClient snsFeignClient;
    @Autowired
    private GroupFeignClient groupFeignClient;
    @Autowired
    private AccountFeignClient accountFeignClient;
    @Autowired
    private OpenimFeignClient openimFeignClient;


    private String getSing() {
        String sign = (String) RedisUtils.getObject("tim_sign");
        if (StringUtils.isBlank(sign)) {
            sign = tlsSigAPIv2.genUserSig(ADMINUSER, 1000 * 60 * 60);
            RedisUtils.setObjectAndTime("tim_sign", sign, 60 * 60);
        }
        return sign;
    }

    private RestParam getQueryPram() {
        RestParam param = new RestParam(sdkAppId, ADMINUSER, getSing(), CONTENTTYPE);
        return param;
    }

    public String importAccount(String account, String nickname) {
        Map<String, Object> map = new HashMap<>();
        map.put("Identifier", account);
        map.put("Nick", nickname);
        map.put("FaceUrl", "http://www.qq.com");
        RestParam param = new RestParam(sdkAppId, ADMINUSER, getSing(), CONTENTTYPE);
        String result = accountFeignClient.importAccount(param, map);
        System.out.println(result);
        return result;
    }

    public FriendAddResult friendAdd(FriendAddForm friendAddForm) {
        RestParam param = new RestParam(sdkAppId, ADMINUSER, getSing(), CONTENTTYPE);
        FriendAddResult result = snsFeignClient.friendAdd(param, friendAddForm);
        return result;
    }

    public FriendDeleteResult friendDelete(FriendDeleteForm form) {
        RestParam param = new RestParam(sdkAppId, ADMINUSER, getSing(), CONTENTTYPE);
        FriendDeleteResult result = snsFeignClient.friendDelete(param, form);
        return result;
    }

    public FriendCheckResult friendCheck(FriendCheckForm form) {
        return snsFeignClient.friendCheck(getQueryPram(), form);
    }

    public GroupAddModel createGroup(CreateGroupForm from) {
        RestParam param = new RestParam(sdkAppId, ADMINUSER, getSing(), CONTENTTYPE);
        GroupAddModel result = groupFeignClient.groupAdd(param, from);
        return result;
    }


    public ImportMsgResult importMsg(ImportMsgForm form) {
        return openimFeignClient.importMsg(getQueryPram(), form);
    }


    public SendMsgResult sendMsg(SendMsgForm form) {
        List<MessageWrapper> messageModels = MessageUtil.messagesFormat(form.getMsgBody());
        form.setMsgBody(messageModels);
        return openimFeignClient.sendMsg(getQueryPram(), form);
    }


}
